package com.unknowpupil.design.sigleton;

/**
 * @Description: <br/>
 * <p>
 *  实现线程安全的双检锁 ：
 *          synchronized和volatile的完美配合，便实现了线程安全的双检锁单例模式。
 * </p>
 * @Author: <a> w10g </a>
 * @CreateDate: Create in 2020/4/24 17:13
 * @Classname: CheckLockSingleton
 * @Version: 1.0
 */
public class CheckLockSingleton {

    /**
     * 先加一把锁
     */
    private volatile static CheckLockSingleton instance;

    /**
     * 无参构造
     */
    public CheckLockSingleton() {
    }

    /**
     *
     * @return
     */
    public static CheckLockSingleton getInstance() {
        if (null == instance) {
            //同步锁
            synchronized (CheckLockSingleton.class) {
                if (null == instance) {

                    instance = new CheckLockSingleton();
                    System.out.println(Thread.currentThread().getName() + ": uniqueInstance is initalized...");
                }else {
                    System.out.println(Thread.currentThread().getName() + ": uniqueInstance is not null now...");
                }

            }
        }
        return instance;
    }
    private Object readResolve() {
        return instance;
    }
}
